<template>
  <div class="divBox">
    <div class="selCard">
      <el-form :model="tableFrom" ref="searchForm" size="small" label-width="85px" :inline="true">
        <el-form-item label="创建时间：">
          <el-date-picker
            v-model="timeVal"
            value-format="yyyy/MM/dd"
            align="right"
            unlink-panels
            format="yyyy/MM/dd"
            size="small"
            type="daterange"
            placement="bottom-end"
            placeholder="自定义时间"
            style="width: 280px;"
            :picker-options="pickerOptions"
            @change="onchangeTime"
          />
        </el-form-item>
        <el-form-item label="上架状态：" prop="is_used">
          <el-select
            v-model="tableFrom.is_used"
            placeholder="请选择"
            class="selWidth"
            clearable
            @change="getList(1)"
          >
            <el-option label="上架" :value="1" />
            <el-option label="下架" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="产品搜索：" prop="keyword">
          <el-input
            v-model="tableFrom.keyword"
            placeholder="请输入产品名称/ID"
            class="selWidth"
            clearable
            @keyup.enter.native="getList(1)"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)">搜索</el-button>
          <el-button size="small" @click="searchReset()">重置</el-button> 
        </el-form-item>
      </el-form>
    </div>
    <el-card class="mt14">
      <div>
        <router-link :to="{ path: `${roterPre}` + '/marketing/integral/addProduct' }">
          <el-button size="small" type="primary">添加积分产品</el-button>
        </router-link>
        <el-button size="small" type="success" @click="quickAdd()" class="ml10">快速添加</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="small"
        class="mt20"
        highlight-current-row
      >
        <el-table-column prop="product_id" label="ID" min-width="50" />
        <el-table-column min-width="100" label="产品图片">
          <template slot-scope="scope">
            <el-image style="width: 36px; height: 36px" :src="scope.row.image" />
          </template>
        </el-table-column>
        <el-table-column prop="store_name" label="产品标题" min-width="200" />
        <el-table-column prop="ot_price" label="兑换积分" min-width="90" />
        <el-table-column prop="price" label="兑换金额" min-width="90" />
        <el-table-column prop="stock" label="库存" min-width="80" />
        <el-table-column prop="sales" label="已兑换数量" min-width="90" />
        <el-table-column prop="create_time" min-width="150" label="创建时间" />
        <el-table-column prop="sort" min-width="90" label="排序" />
        <el-table-column label="状态" min-width="90">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_used"
              :active-value="1"
              :inactive-value="0"
              active-text="上架"
              inactive-text="下架"
              @click.native="onchangeIsShow(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="200" fixed="right">
          <template slot-scope="scope">
            <router-link :to="{ path: `${roterPre}` + '/marketing/integral/orderList?id=' + scope.row.product_id }">
              <el-button type="text" size="small" class="mr10">兑换记录</el-button>
            </router-link>
            <router-link :to="{ path: `${roterPre}` + '/marketing/integral/addProduct/' + scope.row.product_id }">
              <el-button type="text" size="small" class="mr10">编辑</el-button>
            </router-link>
             <router-link :to="{ path: `${roterPre}` + '/marketing/integral/addProduct/' + scope.row.product_id + '?type=2' }">
              <el-button type="text" size="small" class="mr10">复制</el-button>
            </router-link>
            <el-button type="text" size="small" @click="handleDelete(scope.row.product_id, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--快速添加产品-->
    <goods-list ref="goodsList" @getProduct="getProduct"></goods-list>
  </div>
</template>

<script>

import { integralProList, integralProDeleteApi, integralProductStatusApi } from '@/api/marketing';
import { roterPre } from '@/settings';
import timeOptions from '@/utils/timeOptions';
import goodsList from './goodsList'
export default {
  name: 'integralProduct',
  components: {goodsList},
  data() {
    return {
      props: { multiple: false, emitPath: false },
      Loading: false,
      dialogVisible: false,
      roterPre: roterPre,
      listLoading: true,
      receiveType: 0,
      id: '',
      categoryList: [],
      labelList: [],
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        keyword: '',
        is_used: '',
        date: '',
      },
      type: 0,
      isShow: false,
      pickerOptions: timeOptions,
      timeVal: [],
    };
  },
  mounted() {
    this.getList(1);
  },
  methods: {
    /**重置 */
    searchReset(){
      this.timeVal = []
      this.tableFrom.date = ""
      this.$refs.searchForm.resetFields()
      this.getList(1)
    },
    quickAdd(){
      this.$refs.goodsList.dialogVisible = true;
    },
    getProduct(row){
      this.$router.push({ path: this.roterPre + `/marketing/integral/addProduct/${row.id}?type=1` });
    },
    onchangeTime(e) {
     this.timeVal = e
      this.tableFrom.date = e ? this.timeVal.join('-') : ''
      this.tableFrom.page = 1;
      this.getList()
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSureDelete(`删除积分产品后将无法恢复，请谨慎操作!`).then(() => {
        integralProDeleteApi(id)
          .then(({ message }) => {
            this.$message.success(message);
            this.getList('');
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      integralProList(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList('');
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList('');
    },
    // 修改状态
    onchangeIsShow(row) {
      integralProductStatusApi(row.product_id, row.is_used)
        .then(({ message }) => {
          this.$message.success(message);
          this.getList('');
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },
  },
};
</script>

<style scoped lang="scss">

.container {
  min-width: 821px;
}
.dialogBox {
  box-sizing: border-box;
  .el-pagination {
    display: flex;
    justify-content: flex-end;
    margin-top: 25px;
  }
}
</style>
